﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 算法入门.Domain.Operations;
using 算法入门.Utils;

namespace 算法入门.ViewModels.Dialogs
{
    public class SetPermDialogViewModel:BindableBase,IDialogAware
    {

        private User currentUser;
        private string oprType;

        #region 依赖属性

        /// <summary>
        /// 角色列表
        /// </summary>
        private ObservableCollection<PermissionItem> permissionItems;

        public ObservableCollection<PermissionItem> PermissionItems
        {
            get { return permissionItems; }
            set { permissionItems = value; RaisePropertyChanged(); }
        }

        private string userInfo;
        public string UserInfo
        {
            get { return userInfo; }
            set { userInfo = value; RaisePropertyChanged(); }
        }

        #endregion

        #region 命令
        public DelegateCommand OkCommand { get; private set; }
        public DelegateCommand CancelCommand { get; private set; }
        public DelegateCommand CheckAllCommand { get; private set; }
        public DelegateCommand ClearCommand { get; private set; }
        #endregion


        public SetPermDialogViewModel()
        {
            PermissionItems = new ObservableCollection<PermissionItem>(PermissionHelper.GetAllPermissions());

            OkCommand = new DelegateCommand(OkCommandFunc);
            CancelCommand = new DelegateCommand(CancelCommandFunc);

            CheckAllCommand = new DelegateCommand(CheckAllCommandFunc);
            ClearCommand = new DelegateCommand(ClearCommandFunc);

            //UserInfo = "用户Id:4,用户名称:罗纳尔多";
        }

        private void ClearCommandFunc()
        {
            foreach(var perItem in PermissionItems)
            {
                perItem.IsChecked = false;
            }
        }
        private void CheckAllCommandFunc()
        {
            foreach (var perItem in PermissionItems)
            {
                perItem.IsChecked = true;
            }
        }

        private void CancelCommandFunc()
        {
            RequestClose.Invoke(null, ButtonResult.Cancel);
        }

        private void OkCommandFunc()
        {
            //添加权限
            if(oprType== "分配权限")
            {
                //重新赋值权限
                currentUser.PermVal = 0;
                foreach (var perItem in PermissionItems)
                {
                    if(perItem.IsChecked)
                    {
                        var newPerms = PermissionHelper.AddPermission(currentUser.PermVal,(Permission)perItem.PerVal);
                        currentUser.PermVal = newPerms;
                    }
                }
            }
            //删除权限
            else
            {
                foreach (var perItem in PermissionItems)
                {
                    if (perItem.IsChecked)
                    {
                        var newPerms = PermissionHelper.DelPermission(currentUser.PermVal, (Permission)perItem.PerVal);
                        currentUser.PermVal = newPerms;
                    }
                }
            }

            currentUser.RefreshData();

            IDialogParameters parameters = new DialogParameters();
            parameters.Add("returnedUser", currentUser);
            RequestClose.Invoke(parameters, ButtonResult.OK);
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            //CloseWithUserData();
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            currentUser = parameters["User"] as User;
            oprType = parameters["OprType"] as string=="SetPer"?"分配权限":"删除权限";

            foreach(var perItem in PermissionItems)
            {
                if(PermissionHelper.HasPermission(currentUser.PermVal,(Permission)perItem.PerVal))
                {
                    perItem.IsChecked = true;
                }
            }

            UserInfo = $"用户Id:{currentUser.UserId},用户名:{currentUser.UserName},操作类型:{oprType}";
        }

        public DialogCloseListener RequestClose { get; }


        public void CloseWithUserData()
        {
            IDialogParameters parameters = new DialogParameters();
            parameters.Add("returnedUser", currentUser);
            RequestClose.Invoke(parameters,ButtonResult.OK);
        }
    }
}
